<template>
  <el-container class="home">
    <!-- 上面部分 -->
    <el-header>
      <div class="fl">
        <img src="../assets/heima.png" alt="" />
        <span>后台管理系统</span>
      </div>
      <div class="fr">
        <div>
          <el-button type="success" @click="logoout">退出</el-button>
        </div>
      </div>
    </el-header>
    <!-- 下面部分 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 折叠层 -->
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <!-- 第一层 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 第二层 -->
            <el-menu-item
              :index="subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="savePath(subItem.path)"
            >
              <i class="el-icon-lollipop"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [],
      isCollapse: false,
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao',
      },
      activePath: ''
    }
  },
  // 生命周期
  created() {
    this.getMenuList()

    // 调用刚才存储在本地的path 然后再这里获取 再把path给activePath 然后让页面刷新的时候 也能让他继续高亮
    const path = localStorage.getItem('path')
    this.activePath = path
  },
  methods: {
    logoout() {
      localStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      // console.log(res.data)
      this.menulist = res.data
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 解决第二层点击不高亮
    savePath(path) {
      console.log(path)
      // 存在本地以防刷新的时候给去掉
      localStorage.setItem('path', path)
      this.activePath = path
    }
  }
}
</script>
<style scoped lang="less">
.home {
  height: 100%;
  .iconfont {
    margin-right: 15px;
  }
  .el-menu {
    border: none;
  }
  .toggle-button {
    color: #fff;
    text-align: center;
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    // letter-spacing: ; 这个是文字之间的间距 可以是em 也可以是px
    letter-spacing: 0.2em;
    // 鼠标移上去为小手
    cursor: pointer;
  }
  .el-header {
    padding: 0;
    .fl {
      float: left;
      font-size: 20px;
      color: #fff;
      img {
        vertical-align: middle;
        padding-right: 15px;
      }
    }
    .fr {
      float: right;
      margin: 10px 15px;
    }
  }
}
</style>
<style>
.el-header,
.el-footer {
  background-color: #333744;
  color: #333;
}

.el-aside {
  background-color: #333744;
  color: #333;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>
